﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MachiningHelper.ToolFile
{
    public class MachiningPart
    {
        public BorderSeq Border { get; private set; }
        public List<VdrillSeq> Vdirlls { get; private set; }
        public List<HdrillSeq> Hdrills { get; private set; }
        public List<SawSeq> Saws { get; private set; }
        public List<RouteSeq> Routes { get; private set; }
        public List<RouteSeq> Nests { get; private set; }
        public FinishSeq Finish { get; private set; }
        public MachiningPart()
        {
            this.Vdirlls = new List<VdrillSeq>();
            this.Hdrills = new List<HdrillSeq>();
            this.Saws = new List<SawSeq>();
            this.Routes = new List<RouteSeq>();
            this.Nests = new List<RouteSeq>();
        }
        public MachiningPart(string file)
            : this()
        {
            string[] lines = File.ReadAllLines(file, Encoding.GetEncoding("gb2312"));
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                if (line.StartsWith("BorderSequence"))
                {
                    this.Border = BorderSeq.LoadSeq(line);
                }
                else if (line.StartsWith("VdrillSequence"))
                {
                    this.Vdirlls.Add(VdrillSeq.LoadSeq(line));
                }
                else if (line.StartsWith("HDrillSequence"))
                {
                    this.Hdrills.Add(HdrillSeq.LoadSeq(line));
                }
                else if (line.StartsWith("SawSequence"))
                {
                    this.Saws.Add(SawSeq.LoadSeq(line));
                }
                else if (line.StartsWith("RouteSetMillSequence"))
                {
                    RouteSeq route = new RouteSeq(line);
                    for (int x = 1; i + x < lines.Length; x++)
                    {
                        if (lines[i + x].StartsWith("RouteSequence"))
                        {
                            route.AddRoute(lines[i + x]);
                        }
                        else
                        {
                            i = i + x - 1;
                            break;
                        }
                    }
                    this.Routes.Add(route);

                }
                else if (line.StartsWith("NestSetMillSequence"))
                {
                    RouteSeq nest = new RouteSeq(line);
                    for (int x = 1; i + x < lines.Length; x++)
                    {
                        if (lines[i + x].StartsWith("NestSequence"))
                        {
                            nest.AddRoute(lines[i + x]);
                        }
                        else
                        {
                            i = i + x - 1;
                            break;
                        }
                        this.Nests.Add(nest);
                    }
                }
                else
                {
                    this.Finish = FinishSeq.LoadSeq(line);
                }
            }
        }

        private static void read(StreamReader sr, string line, MachiningPart part, out bool isEnd)
        {
            isEnd = false;
            if (line.StartsWith("VdrillSequence"))
            {
                part.Vdirlls.Add(VdrillSeq.LoadSeq(line));
            }
            else if (line.StartsWith("HDrillSequence"))
            {
                part.Hdrills.Add(HdrillSeq.LoadSeq(line));
            }
            else if (line.StartsWith("SawSequence"))
            {
                part.Saws.Add(SawSeq.LoadSeq(line));
            }
            else if (line.StartsWith("RouteSetMillSequence"))
            {
                RouteSeq route = new RouteSeq(line);
                while (((line = sr.ReadLine()) != null) && (line.StartsWith("RouteSequence")))
                {
                    route.AddRoute(line);
                }
                part.Routes.Add(route);
                if (line != null)
                {
                    if (line.StartsWith("EndSequence"))
                    {
                        part.Finish = FinishSeq.LoadSeq(line);
                        isEnd = true;
                    }
                    read(sr, line, part, out isEnd);
                }
            }
            else if (line.StartsWith("EndSequence"))
            {
                part.Finish = FinishSeq.LoadSeq(line);
                isEnd = true;
            }

        }
        public static List<MachiningPart> GetParts(string file)
        {
            List<MachiningPart> parts = new List<MachiningPart>();

            using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding("GB2312")))
            {
                string line;
                bool isEnd;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith("BorderSequence"))
                    {
                        MachiningPart part = new MachiningPart();
                        part.Border = BorderSeq.LoadSeq(line);
                        while ((line = sr.ReadLine()) != null)
                        {
                            read(sr, line, part, out isEnd);
                            if (isEnd)
                            {
                                parts.Add(part);
                                break;
                            }
                        }
                    }
                }
            }
            return parts;
        }
    }
}
